package com.pai4j.ugc.controller;

import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.enums.ResponseCodeEnum;
import com.pai4j.common.util.web.UrlAnalysisUtil;
import com.pai4j.domain.dto.UrlAnalysisResultDTO;
import com.pai4j.domain.vo.request.request.MomentSaveOrUpdateVO;
import com.pai4j.domain.vo.response.moment.MomentDetailVO;
import com.pai4j.domain.vo.response.moment.MomentVO;
import com.pai4j.log.annotation.OperationLog;
import com.pai4j.remote.ugc.MomentClient;
import com.pai4j.ugc.service.moment.MomentService;
import com.pai4j.ugc.service.moment.MomentTimeLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class MomentController implements MomentClient {


    @Autowired
    private MomentService momentService;

    @Autowired
    private MomentTimeLineService momentTimeLineService;

    @Override
    @OperationLog(
        module = "π圈管理",
        operation = "发布动态",
        description = "用户发布π圈动态",
        recordParams = true
    )
    public PAIResponseBean<Long> publish(@RequestBody MomentSaveOrUpdateVO momentSaveOrUpdate) {
        Long momentId = momentService.publish(momentSaveOrUpdate);
        return PAIResponseBeanUtil.success(momentId);
    }

    @Override
    public PAIResponseBean<List<MomentDetailVO>> timeline(String userId, Long lastMomentId, Integer size) {
        List<MomentDetailVO> timeline = momentTimeLineService.timeline(userId, lastMomentId, size);
        return PAIResponseBeanUtil.success(timeline);
    }

    @Override
    public PAIResponseBean<List<MomentDetailVO>> publicTimeline(Long lastMomentId, Integer size) {
        List<MomentDetailVO> timeline = momentTimeLineService.publicTimeline(lastMomentId, size);
        return PAIResponseBeanUtil.success(timeline);
    }

    @Override
    public PAIResponseBean<MomentVO> detail(Long momentId) {
        MomentVO momentVO = momentService.getMomentFromCache(momentId);
        if (momentVO == null) {
            return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(), "动态不存在或已删除");
        }
        return PAIResponseBeanUtil.success(momentVO);
    }


    /**
     * 分页查询指定用户动态（用于我的主页的展示）
     */
    @Override
    public PAIResponseBean<PAIPageResponseBeanUtil<MomentVO>> pageAuthor(Integer pageNo, Integer pageSize, String author) {

        return PAIResponseBeanUtil.success(momentService.pageAuthor(pageNo, pageSize, author));
    }

    /**
     * 链接解析
     */
    @Override
    public PAIResponseBean<UrlAnalysisResultDTO> linkAnalysis(@RequestParam("url") String url) {
        UrlAnalysisResultDTO result = UrlAnalysisUtil.analyze(url);
        return PAIResponseBeanUtil.success(result);
    }

}
